//
//  CommunityMainViewController.m
//  ArtDog
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CommunityMainViewController.h"
#import "BaseCollectionView.h"
#import "CommunityNavigationTitleView.h"
#import "CommunityMainCollectionCell.h"
#import "HLMacro.h"
#import "CommunityWriteTopicController.h"

@interface CommunityMainViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) BaseCollectionView *collectionView;

@property (nonatomic, strong) CommunityNavigationTitleView *titleView;

@end

@implementation CommunityMainViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.hidesBottomBarWhenPushed = YES;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.hidesBottomBarWhenPushed = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createCollectionView];
    [self setupNavigationItem];
    self.isHaveActivity = YES;
}

- (void)setupNavigationItem
{
    _titleView = [[CommunityNavigationTitleView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 100, 44)];
    __weak typeof(self) weakSelf = self;
    _titleView.collectionViewOffset = ^(CGFloat collectionViewOffset) {
        [weakSelf.collectionView setContentOffset:CGPointMake(collectionViewOffset, 0) animated:YES];
    };
    self.navigationItem.titleView = _titleView;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"topic_write"] style:UIBarButtonItemStylePlain target:self action:@selector(writeTopicAction:)];
}

- (void)writeTopicAction:(UIBarButtonItem *)topicItem
{
    CommunityWriteTopicController *topicVC = [[CommunityWriteTopicController alloc]init];
    [self.navigationController pushViewController:topicVC animated:YES];
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) collectionViewLayout:flowLayout];
    [self.view addSubview:_collectionView];
    flowLayout.itemSize = _collectionView.frame.size;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerNib:[UINib nibWithNibName:@"CommunityMainCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"communityMainCollectionCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CommunityMainCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"communityMainCollectionCell" forIndexPath:indexPath];
    cell.type = indexPath.item;
    
    return cell;
}

#pragma mark - scrollView delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _titleView.collectionViewOffset = nil;
    if (scrollView.contentOffset.x / SCREEN_WIDTH == 0) {
        [_titleView recommendAction];
    } else if (scrollView.contentOffset.x / SCREEN_WIDTH == 1) {
        [_titleView bestNewAction];
    } else {
        [_titleView viewerAction];
    }
    __weak typeof(self) weakSelf = self;
    _titleView.collectionViewOffset = ^(CGFloat collectionViewOffset) {
        [weakSelf.collectionView setContentOffset:CGPointMake(collectionViewOffset, 0) animated:YES];
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
